<?php

$__FILEPATH__ = dirname(__FILE__)."/";
require_once($__FILEPATH__."../func_common.php");
require_once($__FILEPATH__."../class_def.php");
require_once($__FILEPATH__."../conf/info.php");
CheckAdminAndInitHeader();

$PageArray = array();
$mysqli = GetMysqliInstance();

if(isset($_POST["send"]))
{
   $file = $_FILES['userfile'];
   if(!JudgeUpLoadImageFile($file)) die("<span style=\"color:red\">failed<br />图片类型不对,只能是gif/jpeg/pjpeg/png/bmp类型的图片</span>");
   if($file["error"]==1)
		die("<span style=\"color:red\">failed<br />filesize over uploadfile_max_filesize</span>");
   else if($file["error"]==2)
        die("<span style=\"color:red\">failed<br />filesize over html_max_file_size</span>");
   else if($file["error"]==3)
		die("<span style=\"color:red\">failed<br />file only uploaded partial</span>");
   else if($file["error"]==4)
        die("<span style=\"color:red\">failed<br />file is not existing</span>");

   $ip = GetRequestIP();
   $size = intval($file["size"]);
   $user = $_SESSION["user"];
   $time = GetSysTime();
   $title = "ppt";
   $extension = GetFilePathExtension($file["name"]);
   $src = "ppt/".md5($time.$file["name"]).$extension;
   move_uploaded_file($file["tmp_name"], "../".$src) or die("failed");
   $strsql="insert into imagelist(src, title, size, user, ip, time) values(?, ?, ?, ?, ?, ?)";
   $stmt = $mysqli->prepare($strsql);
   $stmt->bind_param("ssisss", $src, $title, $size, $user, $ip, $time);
   $stmt->execute();
   $stmt->close();

}
else if(isset($_POST["delete"]))
{
    $src = $_POST["src"];
    $stmt = $mysqli->prepare("delete from imagelist where src=?");
    $stmt->bind_param("s", $src);
    if ($stmt->execute())
    	unlink("../".$src);
    $stmt->close();
} 


// 获取图片列表数据
$result = $mysqli->query("select * from imagelist where title = 'ppt' order by time desc",  MYSQLI_USE_RESULT);
$PageArray["imagelist"] = array();
if ($result)
{
	while($row = $result->fetch_array(MYSQLI_ASSOC))
	{
		$ImageItem = new ImgListItem();
		$ImageItem->id 		= intval($row["id"]);
		$ImageItem->size 	= intval($row["size"]);
		$ImageItem->src 	= $row["src"];
		$ImageItem->title 	= $row["title"];
		$ImageItem->user 	= $row["user"];
		$ImageItem->ip 		= $row["ip"];
		$ImageItem->time 	= $row["time"];

		$PageArray["imagelist"][] = $ImageItem;
	}
	$result->close();
}

$mysqli->close();

?>